package com.glacier.cloud.mq.controller;

import com.glacier.cloud.mq.consts.Constant;
import com.glacier.cloud.mq.entity.dto.cmd.OrderCreateCmd;
import com.glacier.framework.component.exception.BizException;
import jakarta.annotation.Resource;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * date 2023-12-16 16:40
 *
 * @author glacier
 * @version 1.0
 */
@RestController
public class MqController {
    @Resource
    private StreamBridge streamBridge;

    @GetMapping("")
    public String index() {
        return "index";
    }

    @GetMapping("/exp")
    public String exp() {
        throw new BizException("自定义异常");
    }

    @PostMapping("/send1")
    public String send1(@RequestBody OrderCreateCmd cmd) {
        streamBridge.send(Constant.DEMO1_OUT, cmd);
        return "success";
    }

    @GetMapping("/send2")
    public String send2() {
        String sn = UUID.randomUUID().toString().replaceAll("-", "");
        // 发送消息
        Message<String> message = MessageBuilder.withPayload(sn)
                .setHeader("x-delay", 10 * 1000)
                .build();

        streamBridge.send(Constant.DEMO2_OUT, message);
        return "success";
    }

    @GetMapping("/send3")
    public String send3() {
        String sn = UUID.randomUUID().toString().replaceAll("-", "");
        // 发送消息
        Message<String> message = MessageBuilder.withPayload(sn)
                .build();

        streamBridge.send(Constant.DEMO3_OUT, message);
        return "success";
    }

    @GetMapping("/send4")
    public String send4() {
        String sn = UUID.randomUUID().toString().replaceAll("-", "");
        // 发送消息
        Message<String> message = MessageBuilder.withPayload(sn)
                .build();

        streamBridge.send(Constant.DEMO3_OUT, message);
        return "success";
    }

    @GetMapping("/send5")
    public String send5() {
        String sn = UUID.randomUUID().toString().replaceAll("-", "");
        // 发送消息
        Message<String> message = MessageBuilder.withPayload(sn)
                .build();

        streamBridge.send(Constant.DEMO3_OUT, message);
        return "success";
    }
}
